package services;

import domain.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 27/09/12
 * Time: 10:43
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CdaService {

    @Autowired
    private CdaMoleculaDAO cdaMoleculaDAO;
    private CdamasterDAO cdamasterDAO;
    private ProtocolCRUDService protocolCRUDService;

    public CdaService() {
    }

    @Autowired
    public CdaService(CdaMoleculaDAO cdaMoleculaDAO, CdamasterDAO cdamasterDAO, ProtocolCRUDService protocolCRUDService) {
        this.cdaMoleculaDAO = cdaMoleculaDAO;
        this.cdamasterDAO = cdamasterDAO;
        this.protocolCRUDService = protocolCRUDService;
    }

    public void saveMolecula(CdaMolecula cdaMolecula) {
       /* if( cdaMolecula.getActivo() ){
            //List<CdaMolecula> list = cdaMoleculaDAO.findCdaMoleculas(cdaMolecula.getId(), cdaMolecula.getInvolucrado().getCentro().getId());
            cdaMoleculaDAO.updateActivo(cdaMolecula.getInvolucrado().getId());
        }     */
        cdaMoleculaDAO.attachDirty(cdaMolecula);
    }

    public void saveMaster(Cdamaster cdamaster){
       /* if( cdamaster.getActivo() )
            cdamasterDAO.updateActivo(cdamaster.getCentro().getId());    */
        cdamasterDAO.attachDirty(cdamaster);
    }

    public List findAllMolecula(){
        return cdaMoleculaDAO.findAll();
    }


    public List listMolecula(CdaMolecula criteria) {
        List list = findAllMolecula();
        Iterator<CdaMolecula> it = list.iterator();
        while(it.hasNext()){
            CdaMolecula cdaMolecula =  it.next();
            if( criteria.getDroga().getId() != null && !criteria.getDroga().getId().equals(cdaMolecula.getDroga().getId())){
                it.remove();
            }
        }
        return list;
    }


    public List listMaster(Cdamaster criteria) {
        List list = findAllMaster();
        Iterator<Cdamaster> it = list.iterator();
        while(it.hasNext()){
            Cdamaster cdamaster = it.next();
            if( criteria.getCentro().getId() != null && !criteria.getCentro().getId().equals(cdamaster.getCentro().getId())){
                it.remove();
            }
        }
        return list;
    }



    public List findAllMaster(){
        return cdamasterDAO.findAll();
    }

    public Cdamaster findCdaMasterById(Integer id){
       return cdamasterDAO.findById(id);
    }

    public Cdamaster findCdaMasterByCentroId(Integer id){
        return cdamasterDAO.findByCentroId(id);
    }

    public CdaMolecula findCdaMoleculaById(Integer id){
        return cdaMoleculaDAO.findById(id);
    }

    public CdaMolecula findCdaMoleculaByCentroId(Integer id){
        return cdaMoleculaDAO.findByCentroId(id);
    }

    public List<CdaMolecula> findByInvolucradoId(Integer id){
        List<CdaMolecula> cdas = cdaMoleculaDAO.findByInvolucradoId(id);

        /*Me aseguro que las moleculas pertenezcan al protocolo*/
        moleculasDelProtocoloSeleccionado(cdas);

        /*Ordeno por fecha de expiracion, los mas viejos al principio*/
        Collections.sort(cdas);

        if(cdas.size()>0){
            CdaMolecula cdaMenosVencido = cdas.get(cdas.size()-1);

            Iterator<CdaMolecula> it = cdas.iterator();
            while(it.hasNext()){
                CdaMolecula m = it.next();
                /*Si ya se vencio no la saco*/
                if(m.getExpiration().before(new Date())){
                    it.remove();
                }
            }

            /*si me quede sin moleculas muestro la menos vencida*/
            if(cdas.isEmpty()){
                cdas.add(cdaMenosVencido);
            }
        }
        return cdas;
    }

    private void moleculasDelProtocoloSeleccionado(List<CdaMolecula> moleculas){
        try {
            ProtocoloCompromisopais protocoloCompromisopais =
                    protocolCRUDService.recoverProtocoloCompromisopais();

            Set<Droga> drogas = new HashSet<Droga>(protocoloCompromisopais.getProtocolo().getDrogas());
            Iterator<CdaMolecula> it = moleculas.iterator();

            while(it.hasNext()){
                CdaMolecula m = it.next();
                if(!drogas.contains(m.getDroga())){
                    it.remove();
                }
            }
        } catch (NoProtocoloCompromisopaisSelectedException e) {
        }
    }


}
